//
//  XMTopTabSelectView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/10.
//

#import "XMTopTabSelectView.h"

@interface XMTopTabSelectView ()

@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) NSMutableArray *itemBtnArr;

@end

@implementation XMTopTabSelectView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    NSArray *titleArr = @[@"作品", @"收藏", @"喜欢"];
    for (int i = 0; i < 3; i ++) {
        UIButton *itemBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(16.0) frame:CGRectMake((KScaleWidth(58.0) + KScaleWidth(9.0))*i, KScaleWidth(10.0), KScaleWidth(58.0), KScaleWidth(28.0)) title:titleArr[i] selectTitle:titleArr[i] titleColor:kColorHEX(0x8C8C8C) selectColor:kColorHEX(0x333333)];
        [itemBtn addTarget:self action:@selector(itemSelectAction:) forControlEvents:UIControlEventTouchUpInside];
        itemBtn.tag = 4000 + i;
        [self addSubview:itemBtn];
        [self.itemBtnArr addObject:itemBtn];
        if (i == 0) {
            itemBtn.selected = YES;
        }
    }
    [self addSubview:self.lineView];
    _lineView.x = (KScaleWidth(58.0) + KScaleWidth(9.0) - _lineView.width)/2;
    _lineView.y = KScaleWidth(10.0) + KScaleWidth(28.0);
}

//功能选项
- (void)itemSelectAction:(UIButton *)button {
    CGFloat frameX = (button.width - _lineView.width)/2 + button.x;
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.lineView.x = frameX;
    }];
    for (UIButton *btn in self.itemBtnArr) {
        btn.titleLabel.font = KFontSize(16.0);
        btn.selected = NO;
    }
    button.titleLabel.font = KBoldFontSize(16.0);
    button.selected = YES;
    self.itemSelectBlock ? self.itemSelectBlock(button.tag - 4000) : nil;
}

#pragma mark - Init
- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScaleWidth(21.0), 2.0)];
        _lineView.backgroundColor = kColorHEX(0xFED900);
        [_lineView setCornerRadius:1];
    }
    return _lineView;
}

- (NSMutableArray *)itemBtnArr {
    if (!_itemBtnArr) {
        _itemBtnArr = [NSMutableArray array];
    }
    return _itemBtnArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
